package cn.melonlib.cores.web.comp.interceptor;

import cn.melonlib.cores.aop.comp.web.WebInfo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class WebInfoInterceptor implements HandlerInterceptor {

    @Resource
    private WebInfo webInfo;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        webInfo.setServerInfo(new WebInfo.ServerInfo(request.getScheme(),request.getServerName(),request.getLocalAddr(),request.getServerPort(),request.getServletPath(),request.getMethod()));
        webInfo.setClientInfo(new WebInfo.ClientInfo(request.getRemoteAddr(),request.getRemotePort(),request.getHeader("user-agent")));
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        webInfo.removeServerInfo();
        webInfo.removeClientInfo();
    }

}
